/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package eistar.impl;

import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

import eistar.EistarPackage;
import eistar.EvolutionBranch;
import eistar.EvolutionRule;
import eistar.Model;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Evolution Rule</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link eistar.impl.EvolutionRuleImpl#getOriginal <em>Original</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class EvolutionRuleImpl extends EntityImpl implements EvolutionRule {
	/**
	 * The cached value of the '{@link #getOriginal() <em>Original</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getOriginal()
	 * @generated
	 * @ordered
	 */
	protected Model original;
	private EvolutionBranchImpl defaultBranch;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected EvolutionRuleImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EistarPackage.Literals.EVOLUTION_RULE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Model getOriginal() {
		if (original != null && original.eIsProxy()) {
			InternalEObject oldOriginal = (InternalEObject)original;
			original = (Model)eResolveProxy(oldOriginal);
			if (original != oldOriginal) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, EistarPackage.EVOLUTION_RULE__ORIGINAL, oldOriginal, original));
			}
		}
		return original;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Model basicGetOriginal() {
		return original;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setOriginal(Model newOriginal) {
		Model oldOriginal = original;
		original = newOriginal;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EistarPackage.EVOLUTION_RULE__ORIGINAL, oldOriginal, original));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_RULE__ORIGINAL:
				if (resolve) return getOriginal();
				return basicGetOriginal();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_RULE__ORIGINAL:
				setOriginal((Model)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_RULE__ORIGINAL:
				setOriginal((Model)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EistarPackage.EVOLUTION_RULE__ORIGINAL:
				return original != null;
		}
		return super.eIsSet(featureID);
	}

	@Override
	public List<EvolutionBranch> getAllBranches() {
		List<EvolutionBranch> branches = getBranches();
		int probability = getUnchangedProbability();
		if (probability > 0) {
			if (this.defaultBranch == null) {
				this.defaultBranch = new EvolutionBranchImpl() {
					public EvolutionRule getRule() {
						return EvolutionRuleImpl.this;
					}
				};
				this.defaultBranch.setName("@DEFAULT");
			}
			this.defaultBranch.setProbability(probability);
			branches.add(defaultBranch);
		}
		return branches;
	}
	
	@Override
	public List<EvolutionBranch> getBranches() {
		return getMembers(EvolutionBranch.class);
	}
	
	@Override
	public int getUnchangedProbability() {
		int prob = 100;
		for(EvolutionBranch b: getBranches())
			prob -= b.getProbability();
		return prob;
	}
} //EvolutionRuleImpl
